﻿using KwaiXiaoDian.Entity.VO.GroupLeader;
using KwaiXiaoDian.Entity.VO.User;
using KwaiXiaoDian.IService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SSharing.Frameworks.NetCore.Common.Consts;
using SSharing.Frameworks.NetCore.Common.Extends;
using SSharing.Frameworks.NetCore.Common.Helpers;
using SSharing.Frameworks.NetCore.Common.Results;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;

namespace KwaiXiaoDian.OpenApi.Controllers
{
    /// <summary>
    /// 团长管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class LeaderController : ControllerBase
    {
        private readonly IGroupLeaderService _groupLeaderService;
        private readonly IKwaiXiaoDianService _kwaiXiaoDianService;

        public LeaderController(IGroupLeaderService groupLeaderService, IKwaiXiaoDianService kwaiXiaoDianService)
        {
            _groupLeaderService = groupLeaderService;
            _kwaiXiaoDianService = kwaiXiaoDianService;
        }

        /// <summary>
        /// 新增团长
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("add")]
        [Authorize(Roles = "ADMIN")]
        public ActionResult<ApiResult> Add(AddLeaderRequest request)
        {
            var apiResult = new ApiResult();
            var now = DateTime.Now;

            //入参校验
            if (request == null)
            {
                request = new AddLeaderRequest();
            }
            if (request.LeaderName.IsNullOrEmpty())
            {
                apiResult.ErrorMessage = "团长名称不能为空！";
                return apiResult;
            }
            if (request.LeaderId.IsNullOrEmpty())
            {
                apiResult.ErrorMessage = "团长Id不能为空！";
                return apiResult;
            }

            //获取登录用户信息
            var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value.ToLong();
            var role = User.FindFirst(ClaimTypes.Role).Value;

            var addResult = _groupLeaderService.Add(request, userId);
            if (addResult.Item1 != true)
            {
                apiResult.ErrorMessage = addResult.Item2;
                return apiResult;
            }
            apiResult.ErrorCode = ErrorCodeConst.OK;

            return apiResult;
        }

        /// <summary>
        /// 禁用团长
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("disable")]
        [Authorize(Roles = "ADMIN")]
        public ActionResult<ApiResult> Disable(DisableLeaderRequest request)
        {
            var apiResult = new ApiResult();

            //入参校验
            if (request== null)
            {
                request = new DisableLeaderRequest();
            }
            if (request.IsEnabled.IsNullOrEmpty())
            {
                apiResult.ErrorMessage = "禁用状态不能为空!";
                return apiResult;
            }

            //获取登录用户信息
            var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value.ToLong();
            var role = User.FindFirst(ClaimTypes.Role).Value;

            var disableResult = _groupLeaderService.Disable(request, userId);
            if (disableResult.Item1 != true)
            {
                apiResult.ErrorMessage = disableResult.Item2;
                return apiResult;
            }
            apiResult.ErrorCode = ErrorCodeConst.OK;

            return apiResult;
        }

        /// <summary>
        /// 获取团长列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("getpagingleaders")]
        public ActionResult<ApiResult<IPagingResult<Leader>>> GetPagingLeaders(GetPagingLeadersRequest request)
        {
            var apiResult = new ApiResult<IPagingResult<Leader>>();

            //入参校验
            if (request == null)
            {
                request = new GetPagingLeadersRequest()
                {
                    Page = 1,
                    Limit = 10
                };
            }
            if (request.Page <= 0)
            {
                request.Page = 1;
            }
            if (request.Limit <= 0)
            {
                request.Limit = 10;
            }

            var pagingLeaders = _groupLeaderService.GetPagingLeaders(request);
            apiResult.Data = pagingLeaders;
            apiResult.ErrorCode = ErrorCodeConst.OK;

            return apiResult;
        }

        /// <summary>
        /// 查询团长信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("getleaders")]
        public ActionResult<ApiResult<IList<GroupLeaderInfo>>> GetLeaders(GetLeadersRequest request)
        {
            var apiResult = new ApiResult<IList<GroupLeaderInfo>>();

            //入参校验
            if (request == null)
            {
                request = new GetLeadersRequest();
            }
            if (request.LeaderName.IsNullOrEmpty())
            {
                apiResult.ErrorMessage = "团长名称不能为空！";
                return apiResult;
            }

            var leaders = _kwaiXiaoDianService.GetGroupLeaderInfo(new GetGroupLeaderInfoRequest
            {
                Name = request.LeaderName
            });
            apiResult.Data = leaders;
            apiResult.ErrorCode = ErrorCodeConst.OK;

            return apiResult;
        }

    }
}
